package cn.wenhe9.yygh.hosp.service.impl;

import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.hosp.repository.DepartmentRepository;
import cn.wenhe9.yygh.hosp.service.DepartmentService;
import cn.wenhe9.yygh.model.hosp.Department;
import cn.wenhe9.yygh.model.vo.hosp.DepartmentVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author DuJinliang
 * 2022/3/27
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public ResultResponse save(Map<String, Object> paramMap) throws JsonProcessingException {
        String paramMapString = objectMapper.writeValueAsString(paramMap);
        Department department = objectMapper.readValue(paramMapString, Department.class);

        Optional<Department> departmentOptional = departmentRepository.findDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (departmentOptional.isPresent()){
            Department departmentExist = departmentOptional.get();
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

        return ResultResponse.ok();
    }

    @Override
    public ResultResponse findDepartmentByPage(String hoscode, int page, int limit) {
        Pageable pageable = PageRequest.of(page, limit);
        Page<Department> departmentPage = departmentRepository.findAllByHoscodeAndIsDeletedEquals(hoscode, pageable, 0);
        return ResultResponse.ok(departmentPage);
    }

    @Override
    public ResultResponse logicDelete(String hoscode, String depcode) {
        Optional<Department> departmentOptional = departmentRepository.findDepartmentByHoscodeAndDepcodeAndIsDeletedEquals(hoscode, depcode, 0);

        if (departmentOptional.isPresent()){
            Department department = departmentOptional.get();
            department.setIsDeleted(1);
            department.setUpdateTime(new Date());

            departmentRepository.save(department);
        }
        return ResultResponse.ok();
    }

    @Override
    public ResultResponse findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        List<Department> departmentList = departmentRepository.findAllByHoscode(hoscode);

        //根据大科室编号 bigcode分组 获取每个大科室里面下级子科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 departmentMap
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> department1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(department1List.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : department1List) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                children.add(departmentVo2);
            }
            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);
            //放到最终的result里面
            result.add(departmentVo1);
        }

        return ResultResponse.ok(result);
    }

    @Override
    public String getDepname(String hoscode, String depcode) {
        Optional<Department> departmentOptional = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (departmentOptional.isPresent()){
            return departmentOptional.get().getDepname();
        }
        return "";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Optional<Department> departmentOptional = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return departmentOptional.orElse(null);
    }


}
